const dao = require('./base');

module.exports = {

    transferList: function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from ex_transfer where 1=1`;
        let pre = [];
        if (condition.tid !== undefined && condition.tid !== '') {
            sql += ` and a.tid = ?`;
            pre.push(condition.tid);
        }
        if (condition.contract !== undefined && condition.contract !== '') {
            sql += ` and a.contract = ?`;
            pre.push(condition.contract);
        }
        if (condition.to !== undefined && condition.to !== '') {
            sql += ` and a.to = ?`;
            pre.push(condition.to);
        }
        if (condition.from !== undefined && condition.from !== '') {
            sql += ` and a.from = ?`;
            pre.push(condition.from);
        }
        if (condition.txHash !== undefined && condition.txHash !== '') {
            sql += ` and b.txHash = ?`;
            pre.push(condition.txHash);
        }
        sql = dao.by(sql, by);
        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

    transferAdd: function (fields = {}) {
        let sql = "INSERT INTO `ex_transfer` (`txHash`, `from`, `to`, `amount`, `contract`, `blockNumber`) VALUES " +
            "(?,?,?,?,?,?)";
        let pre = [];
        pre.push(fields.txHash);
        pre.push(fields.from);
        pre.push(fields.to);
        pre.push(fields.amount);
        pre.push(fields.contract);
        pre.push(fields.blockNumber);
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

    transferUpdate: async function (condition = {}, fields = {}) {
        let sql = "update sic_bet set ";
        let pre = [];
        // if (fields.bet !== undefined && fields.bet !== '') {
        //     sql = dao.update_sql_splice(sql, "bet");
        //     pre.push(fields.bet);
        // }
        // if (fields.del_flag !== undefined && fields.del_flag !== '') {
        //     sql = dao.update_sql_splice(sql, "del_flag");
        //     pre.push(fields.del_flag);
        // }
        // if (condition.name !== undefined && condition.name !== '') {
        //     sql += ` where name = ?`;
        //     pre.push(condition.name);
        // } else {
        //     throw new Error("condition is null");
        // }

        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err);
        });
    },


};
